package com.ytktt.study.week5.task10.dbms;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 数据库连接操作
 *
 * @author ytk
 */
@Slf4j
public class DatabaseConnection {

    private DataSource dataSource;

    private DatabaseConnection() {
        Properties properties = new Properties();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("hikari.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        String jdbcDriver = properties.getProperty("jdbc.driver");
        String jdbcUrl = properties.getProperty("jdbc.url");
        String databaseUser = properties.getProperty("database.user");
        String databasePassword = properties.getProperty("database.password");
        int minPoolSize = Integer.parseInt(properties.getProperty("pool.min-size"));
        int maxPoolSize = Integer.parseInt(properties.getProperty("pool.max-size"));
        long connectionTimeout = Long.parseLong(properties.getProperty("pool.connection-timeout"));

        if (log.isDebugEnabled()) {
            log.debug("datasource config: [min-pool-size: {}], [max-pool-size: {}], [connection-timeout: {}]",
                    minPoolSize, maxPoolSize, connectionTimeout);
        }

        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(jdbcUrl);
        config.setUsername(databaseUser);
        config.setPassword(databasePassword);
        config.setMinimumIdle(minPoolSize);
        config.setMaximumPoolSize(maxPoolSize);
        config.setConnectionTimeout(connectionTimeout);
        config.setAutoCommit(false);
        this.dataSource = new HikariDataSource(config);
    }

    private static class DatabaseConnectionHolder {
        private final static DatabaseConnection HOLDER = new DatabaseConnection();
    }

    public static final DatabaseConnection getInstance() {
        return DatabaseConnectionHolder.HOLDER;
    }

    /**
     * 原生方式获取连接
     * @param jdbcDriver
     * @param url
     * @param user
     * @param password
     * @return
     */
    public static Connection getConnection(String jdbcDriver, String url, String user, String password) {
        Connection connection;
        try {
            Class.forName(jdbcDriver);
            connection = DriverManager.getConnection(url, user, password);
            connection.setAutoCommit(false);
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    /**
     * 获取数据库连接
     * @return
     */
    public Connection getConnection() throws SQLException {
        return this.dataSource.getConnection();
    }

    public void closeDataSource() {
        ((HikariDataSource) this.dataSource).close();
    }
}
